﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Wage_wages : ATPages
{
   
    ZSql zsql = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
         if(!IsPostBack)
      {
        yjTm1.Attributes.Add("onclick", "SelectDate('yjTm1')");
        yjTm2.Attributes.Add("onclick", "SelectDate('yjTm2')");
        hfxs.Value = Request.QueryString["moduid"].ToString();
        try
        {
            hfuserId.Value = Request.QueryString["StaffName"].ToString();
            hfdata.Value = Request.QueryString["SalesId"].ToString();
            hfgetdep.Value = Request.QueryString["Deptname"].ToString();
            lblwz.Text = f.getModuleidAdd(hfxs.Value);

            gvdata();
            initType1();
          
        }
        catch (Exception)
        {
            lblwz.Text = f.getModuleidAdd(hfxs.Value);
            Linkcannel.Visible = false;
            gvdata();
            initType1();
         
        }
 
       }
       
    }
   
    private void gvdata()
    {
        string sql = "select WagesId,WStaffId,WStaffName,modul,comodName,WStaffSalary,WStaffPrice,WStaffPercentage,WStaffPerformance,WYWage,WSWage,WDiscount,WageDate,AuditUserName,WageDate as AuditDate,department from CS_Wages order by WagesId desc ";
        string sql1 = "select Deptid,WStaffId,WStaffName,sum (WSWage) as daytol,WageDate,department from CS_Wages,Department where department=Deptname group by WStaffName,Deptid,WStaffId,department,WageDate";
        string sql2 = "select Deptid,WStaffId,WStaffSalary,WStaffName,(sum (WSWage)+WStaffSalary) as mtol,sum (WSWage) as daytol,left(WageDate,7) as WageDate,department from CS_Wages,Department where department=Deptname group by WStaffName,Deptid,WStaffId,department,left(WageDate,7),WStaffSalary ";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
        PageNavigator2.Total = 1;
        PageNavigator2.Query = sql1;
        PageNavigator3.Total = 1;
        PageNavigator3.Query = sql2;
       

    }
    
    protected void lnkbtnClearing_Click(object sender, EventArgs e)
    {
        string WageId = (sender as LinkButton).CommandArgument.ToString();

        ClientScript.RegisterStartupScript(typeof(Page), "aa", "PrintData('" + WageId + "')", true);

    }
    protected void btnSelec_Click(object sender, EventArgs e)
    {
        if(yjTm1.Text=="" &&yjTm2.Text!="")
        {
            Response.Write("<script>alert('日期一不能为空');location.href=document.referrer</script>");
        }
        if(yjTm2.Text=="" &&yjTm1.Text!="")
        {
            Response.Write("<script>alert('日期二不能为空');location.href=document.referrer</script>");
            
        }
        
        ZSql selec = new ZSql();
       
            string sql = "select WagesId,Deptid,WStaffId,WStaffName,modul,comodName,WStaffSalary,WStaffPrice,WStaffPercentage,WStaffPerformance,WYWage,WSWage,WDiscount,WageDate,AuditUserName,WageDate as AuditDate,department from CS_Wages,Department where department=Deptname ";
            string sql1 = "select Deptid,WStaffId,WStaffName,sum (WSWage) as daytol,WageDate,department from CS_Wages,Department where department=Deptname ";
            string sql2 = "select Deptid,WStaffId,WStaffSalary,WStaffName,(sum (WSWage)+WStaffSalary) as mtol,sum (WSWage) as daytol,left(WageDate,7) as WageDate,department from CS_Wages,Department where department=Deptname ";

        if (txtselecName.Text == "" && ddrDepartment.SelectedValue == "0" && yjTm1.Text == "" && yjTm2.Text == "")
        {
            sql += "" ;
            sql1 += "";
            sql2 += "";
        }
        else if (ddrDepartment.SelectedValue == "0" && yjTm1.Text == "" && yjTm2.Text == "")
        {
            sql += " and WStaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%'";
            sql1 += " and WStaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%'";
            sql2 += " and WStaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%'";
        }
        else if (txtselecName.Text == "" && yjTm1.Text == "" && yjTm2.Text == "")
        {
            sql += " and Deptid=" + ddrDepartment.SelectedValue + "";
            sql1 += " and Deptid=" + ddrDepartment.SelectedValue + "";
            sql2 += " and Deptid=" + ddrDepartment.SelectedValue + "";
        }
       else if(txtselecName.Text==""&&ddrDepartment.SelectedValue=="0")
        {
            sql += " and WageDate >='" + Common.FormatParameter(yjTm1.Text) + "' and WageDate <='" + Common.FormatParameter(yjTm2.Text) + "'";
            sql1 += " and WageDate >='" + Common.FormatParameter(yjTm1.Text) + "' and WageDate <='" + Common.FormatParameter(yjTm2.Text) + "'";
            sql2 += " and left(WageDate,7) >='" + Common.FormatParameter(yjTm1.Text).Substring(0, 7) + "' and left(WageDate,7) <='" + Common.FormatParameter(yjTm2.Text).Substring(0,7) + "'";
        }
        else if (txtselecName.Text !="" && ddrDepartment.SelectedValue !="0" && yjTm1.Text==""&&yjTm2.Text=="")
        {
            sql += " and WStaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%' and Deptid=" + ddrDepartment.SelectedValue + " ";
            sql1 += " and WStaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%' and Deptid=" + ddrDepartment.SelectedValue + " ";
            sql2 += " and WStaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%' and Deptid=" + ddrDepartment.SelectedValue + " ";
        }
        else if(txtselecName.Text !="" && ddrDepartment.SelectedValue=="0" &&yjTm1.Text!=""&&yjTm2.Text!="")
        {
            sql += " and WStaffName like'%" + txtselecName.Text + "%' and WageDate >='" + Common.FormatParameter(yjTm1.Text) + "' and WageDate <='" + Common.FormatParameter(yjTm2.Text) + "'";
            sql1 += " and WStaffName like'%" + txtselecName.Text + "%' and WageDate >='" + Common.FormatParameter(yjTm1.Text) + "' and WageDate <='" + Common.FormatParameter(yjTm2.Text) + "'";
            sql2 += " and WStaffName like'%" + txtselecName.Text + "%' and left(WageDate,7) >='" + Common.FormatParameter(yjTm1.Text).Substring(0, 7) + "' and left(WageDate,7) <='" + Common.FormatParameter(yjTm2.Text).Substring(0,7) + "'";
        }
        else if (txtselecName.Text == "" && ddrDepartment.SelectedValue != "0" && yjTm1.Text != "" && yjTm2.Text != "")
        {
            sql += " and Deptid=" + ddrDepartment.SelectedValue + " and WageDate >='" + Common.FormatParameter(yjTm1.Text) + "' and WageDate <='" + Common.FormatParameter(yjTm2.Text) + "'";
            sql1 += " and Deptid=" + ddrDepartment.SelectedValue + " and WageDate >='" + Common.FormatParameter(yjTm1.Text) + "' and WageDate <='" + Common.FormatParameter(yjTm2.Text) + "'";
            sql2 += " and Deptid=" + ddrDepartment.SelectedValue + " and left(WageDate,7) >='" + Common.FormatParameter(yjTm1.Text).Substring(0, 7) + "' and left(WageDate,7) <='" + Common.FormatParameter(yjTm2.Text).Substring(0,7) + "'";
        }
        else
        {
            sql += " and WStaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%' and Deptid=" + ddrDepartment.SelectedValue + " and WageDate >='" + Common.FormatParameter(yjTm1.Text) + "' and WageDate <='" + Common.FormatParameter(yjTm2.Text) + "'";
            sql1 += " and WStaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%' and Deptid=" + ddrDepartment.SelectedValue + " and WageDate >='" + Common.FormatParameter(yjTm1.Text) + "' and WageDate <='" + Common.FormatParameter(yjTm2.Text) + "'";
            sql2 += " and WStaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%' and Deptid=" + ddrDepartment.SelectedValue + " and left(WageDate,7) >='" + Common.FormatParameter(yjTm1.Text).Substring(0, 7) + "' and left(WageDate,7) <='" + Common.FormatParameter(yjTm2.Text).Substring(0,7) + "'";
        }

        sql+= "order by WagesId desc";
        sql1 += " group by WStaffName,Deptid,WStaffId,department,WageDate";
        sql2 += " group by WStaffName,Deptid,WStaffId,department,left(WageDate,7),WStaffSalary ";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
        PageNavigator2.Total = 1;
        PageNavigator2.Query = sql1;
        PageNavigator3.Total = 1;
        PageNavigator3.Query = sql2;
    }
    private void initType1()
    {
        ZSql Dep = new ZSql();
        Dep.Open("select * from Department");
        ddrDepartment.DataSource = Dep.m_table;
        ddrDepartment.DataTextField = "Deptname";
        ddrDepartment.DataValueField = "Deptid";
        ddrDepartment.DataBind();
        ddrDepartment.Items.Insert(0, new ListItem("—选择所属部门—", "0"));
        Dep.Close();
    }

    protected void btnback_Click(object sender, EventArgs e)
    {
        
    }



    protected void linkbtnSales_Click(object sender, EventArgs e)
    {
        string WagesId = (sender as LinkButton).CommandArgument.Split('|')[0].ToString();
        string WageDate = (sender as LinkButton).CommandArgument.Split('|')[1].ToString();
        Response.Redirect("WageUpdate.aspx?moduid=" + hfxs.Value + "&WagesId=" + WagesId + "&WageDate="+WageDate+"");
    }
    protected void btnPrint_Click(object sender, EventArgs e)
    {
        string WageId, gettim1, gettim2, name, selectId;
       
        if (txtselecName.Text == "" && ddrDepartment.SelectedValue == "0" && yjTm1.Text == "" && yjTm2.Text == "")
        {
            WageId = "All";
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "AllPrintData('" + WageId + "')", true);
        }
   
        else
        {
            WageId = "tj";
            name = txtselecName.Text;
            gettim1 = yjTm1.Text;
            gettim2 = yjTm2.Text;
            selectId = ddrDepartment.SelectedValue;
           
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "AllPrintData('"+WageId+"','" + gettim1 + "','" + gettim2 + "','" + name + "','" + selectId + "')", true);
        }
      
        
    }
    protected void Linkcannel_Click(object sender, EventArgs e)
    {
        Response.Redirect("wages.aspx?moduid=" + hfxs.Value + "");
    }

    protected void GvDataVW_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], Request["moduid"], 12, GvDataVW);
        biaoji();
    }
    private void biaoji()
    {
        ZSql jt = new ZSql();
        if (GvDataVW.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataVW.Rows.Count; i++)
            {
                string id = GvDataVW.Rows[i].Cells[0].Text.ToString();

                    jt.Open("select * from CS_Wages where WagesId=" + id + "");
                    if (jt.m_table.Rows.Count > 0)
                    {
                        if (jt.m_table.Rows[0]["AuditState"].ToString() == hfdata.Value)
                        {
                           GvDataVW.Rows[i].BackColor = System.Drawing.ColorTranslator.FromHtml("#ECF9FC");
                        }
                    }
                    
            
                //if (hfdata.Value == "")
                //{
                   
                //    if (hfuserId.Value == StaffName && hfgetdep.Value == dep)
                //    {

                //        GvDataVW.Rows[i].BackColor = System.Drawing.ColorTranslator.FromHtml("#ECF9FC");

                //    }
                //}
            }
        }
    }
    protected void btndel_Click(object sender, EventArgs e)
    {
       
   
        ZSql del = new ZSql();
        int n = 0;
        if (this.GvDataVW.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataVW.Rows.Count; i++)
            {
                CheckBox ck = GvDataVW.Rows[i].Cells[8].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {

                    del.Open("delete CS_Wages  where WagesId='" + this.GvDataVW.Rows[i].Cells[0].Text + "'");
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                   
                }
                else
                {
                    n++;
                }

                if (n == this.GvDataVW.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }

            }
        }
    }

    protected void btnPrintm_Click(object sender, EventArgs e)
    {
        string WageId, gettim1, gettim2, name, selectId;

        if (txtselecName.Text == "" && ddrDepartment.SelectedValue == "0" && yjTm1.Text == "" && yjTm2.Text == "")
        {
            WageId = "All";
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "AllPrintData1('" + WageId + "')", true);
        }

        else
        {
            WageId = "tj";
            name = txtselecName.Text;
            gettim1 = yjTm1.Text;
            gettim2 = yjTm2.Text;
            selectId = ddrDepartment.SelectedValue;

            ClientScript.RegisterStartupScript(typeof(Page), "aa", "AllPrintData1('" + WageId + "','" + gettim1 + "','" + gettim2 + "','" + name + "','" + selectId + "')", true);
        }
    }
    protected void btnalPrints_Click(object sender, EventArgs e)
    {
        string WageId, gettim1, gettim2, name, selectId;

        if (txtselecName.Text == "" && ddrDepartment.SelectedValue == "0" && yjTm1.Text == "" && yjTm2.Text == "")
        {
            WageId = "All";
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "AllPrintData2('" + WageId + "')", true);
        }

        else
        {
            WageId = "tj";
            name = txtselecName.Text;
            gettim1 = yjTm1.Text;
            gettim2 = yjTm2.Text;
            selectId = ddrDepartment.SelectedValue;

            ClientScript.RegisterStartupScript(typeof(Page), "aa", "AllPrintData2('" + WageId + "','" + gettim1 + "','" + gettim2 + "','" + name + "','" + selectId + "')", true);
        }
    }
}
